//
//  ZHCollectionViewHelper.m
//  ArthurRoom
//
//  Created by 赵宏亚 on 2019/8/16.
//  Copyright © 2019 ArthurRoom. All rights reserved.
//

#import "ZHCollectionViewHelper.h"

@interface ZHCollectionViewHelper ()
@property (nonatomic, strong) NSString * cellIdentifier;
@end

@implementation ZHCollectionViewHelper

+ (instancetype)bindWithCollectionView:(UICollectionView *)collectionView dataSource:(RACSignal *)source selectionCommand:(RACCommand *)command templateCellClassName:(NSString *)classCell {
    return [[self alloc] initWithCollectionView:collectionView dataSource:source selectionCommand:command templateCellClassName:classCell];
}

//RAC
+ (instancetype)bindWithCollectionView:(UICollectionView *)collectionView dataSource:(RACSignal *)source selectionCommand:(RACCommand *)command templateCell:(UINib *)nibCell {
    return [[self alloc] initWithCollectionView:collectionView dataSource:source selectionCommand:command templateCell:nibCell];
}

- (instancetype)initWithCollectionView:(UICollectionView *)collectionView dataSource:(RACSignal *)source selectionCommand:(RACCommand *)command templateCell:(UINib *)nibCell {
    self = [self initWithCollectionView:collectionView dataSource:source selectionCommand:command];
    if (!self) return nil;
    
    _templateCell = [[nibCell instantiateWithOwner:nil options:nil] firstObject];
    _cellIdentifier = _templateCell.reuseIdentifier;
    [_collectionView registerNib:nibCell forCellWithReuseIdentifier:_cellIdentifier];
    
    return self;
}

- (instancetype)initWithCollectionView:(UICollectionView *)collectionView dataSource:(RACSignal *)source selectionCommand:(RACCommand *)command templateCellClassName:(NSString *)classCell {
    self = [self initWithCollectionView:collectionView dataSource:source selectionCommand:command];
    if (!self) return nil;
    
    self.cellIdentifier = classCell;
    [_collectionView registerClass:NSClassFromString(classCell) forCellWithReuseIdentifier:_cellIdentifier];
    
    return self;
}

- (instancetype)initWithCollectionView:(UICollectionView *)collectionView dataSource:(RACSignal *)source selectionCommand:(RACCommand *)command {
    NSParameterAssert(collectionView);
    NSParameterAssert(source);
    self = [super init];
    if (!self) return nil;
    
    _collectionView = collectionView;
    _selectCommand = command;
    _data = [NSMutableArray array];
    
    @weakify(self)
    [source subscribeNext:^(NSArray *dataList) {
        @strongify(self)
        self.data = [NSMutableArray arrayWithArray:dataList];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
            [self.collectionView reloadData];
        });
    }];
    
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    return self;
}

#pragma mark - DataSource and Delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [self dequeueCellAndBindInCollectionView:collectionView indexPath:indexPath];
}

- (UICollectionViewCell *)dequeueCellAndBindInCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath {
    id<ZHCellDelegate> cell = [collectionView dequeueReusableCellWithReuseIdentifier:_cellIdentifier forIndexPath:indexPath];
    [cell cellWithModel:_data[indexPath.row]];
    return (UICollectionViewCell *)cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [_selectCommand execute:_data[indexPath.row]];
}

#pragma mark - Custon Action
- (void)reloadDataWithSourceList:(NSArray *)source
{
    if (source) {
        _data = [NSMutableArray arrayWithArray:source];
    }
    [_collectionView reloadData];
}





@end
